package com.ms.biz.init;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.system.comm.utils.FrameStringUtil;
import com.system.dao.BaseDao;
import com.system.dao.utils.DbUtil;
import com.system.ds.DbContextHolder;

/**
 * 脚本升级
 * @author yuejing
 * @date 2018年12月18日 上午11:17:25
 */
public class DbUpdate {

	private static final Logger LOGGER = LoggerFactory.getLogger(DbUpdate.class);

	/**
	 * 初始化更新后的脚本
	 */
	public static void init() {
		/*if (EnvUtil.projectDbUpdate() != 1) {
			return;
		}*/
		/*ThreadPoolTaskExecutor task = FrameSpringBeanUtil.getBean(ThreadPoolTaskExecutor.class);
		task.execute(new Runnable() {

			@Override
			public void run() {*/
		version1_0_0();
		/*}

		});*/
	}
	
	/*private static void version2_0_1(BaseDao baseDao) {
	String sql = null;
	// 任务表增加字段: 失败发送邮件规则
	List<TableField> fields = baseDao.tableFields("task_job");
	boolean isFailemailrule = false;
	for (TableField f : fields) {
		if ("failemailrule".equalsIgnoreCase(f.getName())) {
			isFailemailrule = true;
		}
	}
	if (!isFailemailrule) {
		LOGGER.info("|===================================================");
		LOGGER.info("|========== 表[task_job]增加字段: failemailrule");
		sql = "alter table task_job add failemailrule varchar(250) comment '失败发送邮件规则'";
		baseDao.updateSql(sql);
		LOGGER.info("|===================================================");
	}

}*/
	private static void version1_0_0() {
		DbContextHolder.setDsName("dataSource1");
		BaseDao baseDao = new BaseDao();
		if (DbUtil.isMysql()) {
			//createSysDictTypeMysql(baseDao);
			//createSysDictMysql(baseDao);
		} else if (DbUtil.isOracle()) {
		}
	}

	private static void createSysDictTypeMysql(BaseDao baseDao) {
		try {
			baseDao.queryForLong("select count(*) from sys_dict_type limit 1");
		} catch (Exception e) {
			LOGGER.info("|===================================================");
			LOGGER.error("|========== 不存在表[sys_dict_type]: " + e.getMessage());
			String sql = "CREATE TABLE sys_dict_type (\r\n" + 
					"				code varchar(32) NOT NULL COMMENT '编码',\r\n" + 
					"				name varchar(100) NOT NULL COMMENT '名称',\r\n" + 
					"			  	parent_code varchar(32) default '0' COMMENT '上级编码',\r\n" + 
					"				remark varchar(200) default NULL COMMENT '备注',\r\n" + 
					"				create_user_id varchar(32) NOT NULL COMMENT '添加人',\r\n" + 
					"				create_time datetime NOT NULL COMMENT '添加时间',\r\n" + 
					"			  	PRIMARY KEY (code),\r\n" + 
					"  				UNIQUE KEY unique_name (name)\r\n" + 
					"			) COMMENT='系统字典分类表';";
			batchExec(sql, baseDao);
			LOGGER.info("|===================================================");
		}
	}
	private static void createSysDictMysql(BaseDao baseDao) {
		try {
			baseDao.queryForLong("select count(*) from sys_dict limit 1");
		} catch (Exception e) {
			LOGGER.info("|===================================================");
			LOGGER.error("|========== 不存在表[sys_dict]: " + e.getMessage());
			String sql = "CREATE TABLE sys_dict (\r\n" + 
					"			  	dict_id varchar(32) NOT NULL COMMENT '编号',\r\n" + 
					"			  	type_code varchar(32) NOT NULL COMMENT '类型编码',\r\n" + 
					"			  	name varchar(50) NOT NULL COMMENT '名称',\r\n" + 
					"			  	value varchar(200) NOT NULL COMMENT '值',\r\n" + 
					"			  	remark varchar(200) default NULL COMMENT '备注',\r\n" + 
					"			  	create_user_id varchar(32) NOT NULL COMMENT '添加人',\r\n" + 
					"			  	create_time datetime NOT NULL COMMENT '添加时间',\r\n" + 
					"			  	PRIMARY KEY  (dict_id, type_code)\r\n" + 
					"			) COMMENT='系统字典值表';";
			batchExec(sql, baseDao);
			LOGGER.info("|===================================================");
		}
	}
	
	private static void batchExec(String sql, BaseDao baseDao) {
		List<String> sqls = FrameStringUtil.toArray(sql, ";");
		for (String s : sqls) {
			if (FrameStringUtil.isEmpty(s)) {
				continue;
			}
			baseDao.updateSql(s);
		}
	}
}
